<template>
  <div class="electricity-container">
    <!-- Solar Radiation Section -->
    <div class="form-section">
      <button class="distribution-btn device-submit-btn" @click="submitAllDeviceParams">
        <span class="btn-text">提交设备参数</span>

      </button>
      <h2 class="title">电需求</h2>
      <div class="input-group">
        <label class="input-label">电力损失成本</label>
        <div class="input-controls">
          <div class="input-wrapper">
            <input type="number" v-model="xuqiu1" class="form-input">
            <span class="unit">(RMB/kWh)</span>
          </div>
        </div>
      </div>
      <div class="input-group">
        <label class="input-label">电转生物质能损失成本</label>
        <div class="input-controls">
          <div class="input-wrapper">
            <input type="number" v-model="xuqiu2" class="form-input">
            <span class="unit">(RMB/kWh)</span>
          </div>
        </div>
      </div>
      <div class="input-group">
        <label class="input-label">用于交通的电能损失成本</label>
        <div class="input-controls">
          <div class="input-wrapper">
            <input type="number" v-model="xuqiu3" class="form-input">
            <span class="unit">(RMB/kWh)</span>
          </div>
        </div>
      </div>

    <h2 class="title">热需求</h2>
      <div class="input-group">
        <label class="input-label">热需求损失成本</label>
        <div class="input-controls">
          <div class="input-wrapper">
            <input type="number" v-model="xuqiu4" class="form-input">
            <span class="unit">(RMB/kWh)</span>
          </div>
        </div>
      </div>

    <h2 class="title">冷需求</h2>
      <div class="input-group">
        <label class="input-label">冷需求损失成本</label>
        <div class="input-controls">
          <div class="input-wrapper">
            <input type="number" v-model="xuqiu5" class="form-input">
            <span class="unit">(RMB/kWh)</span>
          </div>
        </div>
      </div>

    <h2 class="title">工业领域能源需求</h2>
      <div class="input-group">
        <label class="input-label">天然气损失成本</label>
        <div class="input-controls">
          <div class="input-wrapper">
            <input type="number" v-model="xuqiu6" class="form-input">
            <span class="unit">(RMB)</span>
          </div>
        </div>
      </div>
      <div class="input-group">
        <label class="input-label">生物质能损失成本</label>
        <div class="input-controls">
          <div class="input-wrapper">
            <input type="number" v-model="xuqiu7" class="form-input">
            <span class="unit">(RMB)</span>
          </div>
        </div>
      </div>
      <div class="input-group">
        <label class="input-label">氢气损失成本</label>
        <div class="input-controls">
          <div class="input-wrapper">
            <input type="number" v-model="xuqiu8" class="form-input">
            <span class="unit">(RMB)</span>
          </div>
        </div>
      </div>

      <h2 class="title">交通领域能源需求</h2>
      <div class="input-group">
        <label class="input-label">天然气损失成本</label>
        <div class="input-controls">
          <div class="input-wrapper">
            <input type="number" v-model="xuqiu9" class="form-input">
            <span class="unit">(RMB)</span>
          </div>
        </div>
      </div>
      <div class="input-group">
        <label class="input-label">电力损失成本(无V2G)</label>
        <div class="input-controls">
          <div class="input-wrapper">
            <input type="number" v-model="xuqiu10" class="form-input">
            <span class="unit">(RMB/kWh)</span>
          </div>
        </div>
      </div>
      <div class="input-group">
        <label class="input-label">电力损失成本(V2G)</label>
        <div class="input-controls">
          <div class="input-wrapper">
            <input type="number" v-model="xuqiu11" class="form-input">
            <span class="unit">(RMB/kWh) </span>
          </div>
        </div>
      </div>
    </div>
  </div>

</template>

<script lang="ts" setup>
import { ref, onMounted, type Ref } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const submitAllDeviceParams = async () => {
  try {
    const payloads = [
 { id: 1, value: parseFloat(xuqiu1.value) || 0, url: 'dianxq' },
  { id: 2, value: parseFloat(xuqiu2.value) || 0, url: 'dianxq' },
  { id: 3, value: parseFloat(xuqiu3.value) || 0, url: 'dianxq' },
  { id: 1, value: parseFloat(xuqiu4.value) || 0, url: 'rexq' },
  { id: 2, value: parseFloat(xuqiu5.value) || 0, url: 'lengxq' },
  { id: 3, value: parseFloat(xuqiu6.value) || 0, url: 'gongyexq' },
  { id: 1, value: parseFloat(xuqiu7.value) || 0, url: 'gongyexq' },
  { id: 2, value: parseFloat(xuqiu8.value) || 0, url: 'gongyexq' },
  { id: 3, value: parseFloat(xuqiu9.value) || 0, url: 'jiaotongxq' },
  { id: 1, value: parseFloat(xuqiu10.value) || 0, url: 'jiaotongxq' },
  { id: 2, value: parseFloat(xuqiu11.value) || 0, url: 'jiaotongxq' },
  ]

    for (const item of payloads) {
      await axios.put(`http://localhost:8080/${item.url}`, {
        id: item.id,
        value: item.value
      })
    }

    ElMessage.success('所有设备参数提交成功')
  } catch (err) {
    ElMessage.error('提交失败，请检查网络或数据')
    console.error(err)
  }
}

const fetchAndSet = async (url: string, variable: Ref<string>) => {
  try {
    const response = await axios.get(url);
    variable.value = response.data.value;
  } catch {
    ElMessage.error(`加载 ${url} 失败`);
  }
};

onMounted(async () => {

    await fetchAndSet('http://localhost:8080/dianxq/97', xuqiu1);
    await fetchAndSet('http://localhost:8080/dianxq/98', xuqiu2);
    await fetchAndSet('http://localhost:8080/dianxq/99', xuqiu3);
    await fetchAndSet('http://localhost:8080/rexq/9', xuqiu4);
    await fetchAndSet('http://localhost:8080/lengxq/9', xuqiu5);
    await fetchAndSet('http://localhost:8080/gongyexq/9', xuqiu6);
    await fetchAndSet('http://localhost:8080/gongyexq/9', xuqiu7);
    await fetchAndSet('http://localhost:8080/gongyexq/9', xuqiu8);
    await fetchAndSet('http://localhost:8080/jiaotongxq/9', xuqiu9);
    await fetchAndSet('http://localhost:8080/jiaotongxq/9', xuqiu10);
    await fetchAndSet('http://localhost:8080/jiaotongxq/9', xuqiu11);

})


// Hydrogen Storage
    const xuqiu1 = ref('')
    const xuqiu2 = ref('')
    const xuqiu3 = ref('')
    const xuqiu4 = ref('')
    const xuqiu5 = ref('')
    const xuqiu6 = ref('')
    const xuqiu7 = ref('')
    const xuqiu8 = ref('')
    const xuqiu9 = ref('')
    const xuqiu10 = ref('')
    const xuqiu11 = ref('')

</script>

<style scoped>
.electricity-container {
  background-color: #f8fafc;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  border-radius: 14px;
  padding: 36px;
  max-width: 920px;
  margin: 0 auto;
  font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
  color: #1e293b;
}

.title {
  color: #4c1d95;
  text-align: center;
  margin-bottom: 32px;
  font-size: 28px;
  font-weight: 700;
  padding-bottom: 16px;
  border-bottom: 2px solid #7c3aed;
  letter-spacing: 0.5px;
}

.section-title {
  color: #475569;
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #e2e8f0;
}

.form-section, .info-section {
  background-color: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 28px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.input-group {
  display: flex;
  margin-bottom: 18px;
  gap: 16px;
}

.input-label {
  min-width: 180px;
  font-weight: 500;
  color: #475569;
  padding-top: 10px;
}

.input-controls {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.input-wrapper {
  display: flex;
  align-items: center;
  /* background-color: #f8fafc; */
  border-radius: 8px;
  padding: 2px;

}

.form-input {
  padding: 10px 16px;
  border: 1px solid #053676;
  border-radius: 6px;
  width: 100px;
  text-align: right;
  font-size: 15px;
  background: transparent;
  color: #1e293b;
}

.form-input.small {
  width: 100px;
}

.form-input:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(124, 58, 237, 0.2);
}

.unit {
  margin: 0 5px;
  color: #64748b;
  font-size: 14px;
  font-weight: 500;
}

.distribution-btn {
  background-color: #7c3aed;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s;
  height: 42px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;

}

.distribution-btn:hover {
  background-color: #6d28d9;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(124, 58, 237, 0.3);
}

.btn-text {
  font-size: 14px;
  line-height: 1.2;
}

.file-name {
  color: rgba(255, 255, 255, 0.8);
  font-size: 11px;
  font-style: italic;
  margin-top: 2px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 18px;
}

.info-card {
  background-color: #f8fafc;
  padding: 18px;
  border-radius: 10px;
  border-left: 4px solid #7c3aed;
  transition: transform 0.2s, box-shadow 0.2s;
}

.info-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.info-label {
  display: block;
  font-weight: 500;
  color: #475569;
  margin-bottom: 10px;
  font-size: 15px;
}

.info-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.info-value {
  font-weight: 600;
  color: #4c1d95;
  font-size: 15px;
  white-space: nowrap;
}

@media (max-width: 768px) {
  .electricity-container {
    padding: 24px;
    border-radius: 0;
  }

  .input-group {
    flex-direction: column;
    gap: 8px;
  }

  .input-label {
    min-width: auto;
    padding-top: 0;
    margin-bottom: 6px;
  }

  .input-controls {
    width: 100%;
  }

  .form-input {
    width: 100%;
  }

  .info-grid {
    grid-template-columns: 1fr;
  }
}
</style>

